<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class rebuild extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
	var $model = "mrebuild";
	var $view = "rebuild";
	var $resource = "";
    function __construct() {
        parent::__construct();
        $this->load->model($this->model);
        $this->load->model("mcategory");
        $this->load->model("mrebuild");
		$this->resCode = $this->{$this->model}->resCode;
        $this->url_index = admin_url() . $this->view;
        $this->folder_upload = "dataupload/".$this->view."/";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
		$this->url_search = $this->url_index . "/search";
		$this->resource = $this->mresource->get($this->resCode,'RES_CODE');
    }

    public function index($page = 0) {
		$this->check_permission();
        $data = array();
		$page = ($page <= 0) ? 1 : $page;
        $config = array();
        $config['per_page'] = 20;
        $config['cur_page'] = $page;
        $config['base_url'] = $this->url_index.'/index/';
	//	$this->mrebuild->rebuild_user_rank();
		//$this->mrebuild->rebuild_game_score_rank(4);
		//$this->mrebuild->rebuild_category_count(array(2,5));
		 
        $data['datas'] = $this->{$this->model}->pagination('','',$config);
        $this->theme_data["main"] = $this->view."/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($editID = 0) {
		$this->check_permission($editID);
		$this->load->model("mgameinstruction");
        $data = array();
        $post = $this->input->post();
        if ($post) {
			if($post['edit']['game_code'] ==''){
					$post['edit']['game_code'] = $this->sseo->seolink($post['edit']['game_title']);
			}
            if ($editID > 0) {
				$arr_cate_update =array();
				$this->mgamecategory->delete($editID,'game_id');
				
				$this->mgamecategory->insert_category_by_gameid($editID,$post['cat_id']);
                $this->{$this->model}->update($editID, $this->{$this->model}->primaryKey, $post['edit']);
            }
			else{
				
				$editID = $this->{$this->model}->insert($post['edit']);
			}
            redirect($this->url_edit . $editID);
        }
		
		$data['edit'] = $this->{$this->model}->get($editID);
		
        $this->theme_data["main"] = $this->view."/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
}
